package com.crud.project.dao;

import com.crud.project.service.ShrioService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

public class CustomRealm extends AuthorizingRealm {
    @Autowired
    private ShrioService shrioService;
    //权限
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //从主体获取传过来的用户
        String username=(String)authenticationToken.getPrincipal();
        //通过用户名从数据库查询密码
        String password=shrioService.findUserByName(username);

        if (password==null){return null;}
        //new 一个HashedCredentialsMatcher继承的算法对象，传入参数，实现bean定义的算法，然后把对象封装返回到
        SimpleAuthenticationInfo simpleAuthenticationInfo=new SimpleAuthenticationInfo(username, password,this.getName());

        return simpleAuthenticationInfo;
    }
}
